//
//  ActivetableView.m
//  Hero
//
//  Created by lanou3g on 16/5/4.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "ActivetableView.h"
#import "YQNetWorkRequestManager.h"
#import "ConsultModel.h"
#import "ConsultTableViewCell.h"
@interface ActivetableView ()<UITableViewDataSource,UITableViewDelegate>

//@property (nonatomic, assign) int count;
@end
@implementation ActivetableView
static int countt = 1;
- (void)dealloc
{
    [self removeObserver:[self defaultFooter] forKeyPath:@"contentOffset"];
    [self removeObserver:[self defaultFooter] forKeyPath:@"contentSize"];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.dataArray1 = [NSMutableArray new];
        self.dataArray = [NSMutableArray new];
        [self reloadData1:countt];
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = 0;
        __weak typeof(self)weakSelf = self;
        [self addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
            [weakSelf reloadData1:++countt];

        }];
    }
    return self;
}
- (void)reloadData1:(int)count
{

    [YQNetWorkRequestManager requestURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://qt.qq.com/static/pages/news/phone/c73_list_%d.shtml",count]] method:YQ_GET params:nil result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {

            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dic  in dict[@"list"]) {
                ConsultModel *model = [ConsultModel new];
                [model setValuesForKeysWithDictionary:dic];
                [self.dataArray1 addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self reloadData];
                [[self defaultFooter] endRefreshing];
            });
        }
    }];
     [self registerNib:[UINib nibWithNibName:@"ConsultTableViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ConsultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ConsultModel *model = self.dataArray[indexPath.row];
    [self.myDelegate didSelectCellDeledateWithModel:model];
}

@end
